var targetObj; //生成的目标target容器
var form;
var selectArr;
$(function () {
    form = $('#xh_form');
    getType();
    getPurpose();
    bindTypeSelect();//点击型号
    bindDestinationSelect();//点击目的地

    function bindDestinationSelect() {
        $('#iDestination').unbind('change').on('change',function () { //类型点击事件
            var text =$(this).find("option:selected").text();
            var str ='';

            var now = new Date();
            var day = ("0" + now.getDate()).slice(-2);//格式化月，如果小于9，前面补0
            var month = ("0" + (now.getMonth() + 1)).slice(-2);//拼装完整日期格式
            var today = now.getFullYear()+"-"+(month)+"-"+(day);

            if(targetObj != null){
                targetObj.remove();
            }
            if(text !== '公司'){
                str = '<tr>' +
                    '<td><label>快递公司:</label></td><td><input id="expressCompany" name="expressCompany" class="form-control" placeholder="如申通、顺丰等等，必填"/></td>' +
                    '<td><label>送修时间:</label></td><td><input type="date" id="repairDate" name="repairDate" class="form-control" value="'+today+'"/></td>' +
                    '</tr>';
                str += '<tr>' +
                    '<td><label>快递单号:</label></td><td colspan="3"><input id="expressSend" name="expressSend" class="form-control" placeholder="必填"/></td>';
                targetObj  = $(str);
                form.find('#sure').remove();
                form.find('table tbody').append(targetObj);
            }
        });
    }

    function bindTypeSelect() {
        $('#sModel').unbind('change').on('change',function () { //类型点击事件
            getProd($(this).val());
        });
    }

    function getType() {
        // $.ajax({
        //     async: false,
        //     url:baseUrl+'School/getUser',
        //     method:'post',
        //     data:{userType: 1, schoolId: $('#schoolId_data').val(), userIds: $('#id_data').val()},
        //     dataType:'json',
        //     success:function (data) {
        //         if (data.total == 1){
        //             $('#sModel_data').val(data.rows[0].sModel);
        //             $('#sImei_data').val(data.rows[0].sImei);
        //             $('#sDeviceNumber').val(data.rows[0].sDeviceNumber);
        //         } else {
        //             window.top.xhTop.showError('获取学生信息错误！');
        //         }
        //     }
        // });
        $.ajax({
            async: false,
            url:baseUrl+'device/getTabletData',
            method:'post',
            data:{model:$('#sModel_data').val()},
            dataType:'json',
            success:function (data) {
                fillSelect('sModel',data.model,null,null,false);
                if (data.Imei !== null && data.Imei != 'undefined') {
                    document.getElementById("sBrand").value=data.Imei;
                }
            }
        });
    }

    function getPurpose() {
        $.ajax({
            url:baseUrl+'device/getPurposeData',
            method:'post',
            dataType:'json',
            success:function (data) {
                fillSelect('purpose',data,null,null,true);
            }
        });
    }

    function getProd(id,$value) {
        $.ajax({
            url:baseUrl+'device/getBrandData',
            method:'post',
            data:{model:id},
            dataType:'json',
            success:function (data) {
                document.getElementById("sBrand").value=data;
            }
        });
    }

    function fillSelect(selector,data,id,text,other) {
        var str = '<option value="" style="display: none" selected>请选择</option>';
        if(id != null && text != null){
            $.each(data,function (key,val) {
                str += '<option value="'+val[id]+'">'+val[text]+'</option>';
            })
        } else {
            if (other === true){
                $.each(data,function (key,val) {
                    if (key === '1'){
                        str += '<option value="'+key+'" selected>'+val+'</option>';
                    }else{
                        str += '<option value="'+key+'">'+val+'</option>';
                    }
                })
            } else {
                $.each(data,function (key,val) {
                    if (val === $('#sModel_data').val()){
                        str += '<option value="'+key+'" selected>'+val+'</option>';
                    }else{
                        str += '<option value="'+key+'">'+val+'</option>';
                    }
                })
            }
        }
        $('#'+selector).empty().append(str);
    }
    $('#bt_modal_confirm',parent.document).click(function () {
        var userId = $('#id_data').val();
        var logonName = $('#sLogonName_data').val();
        var userName = $('#sUserName_data').val();
        var sImei = $('#sImei_data').val();
        var schoolId = $('#schoolId_data').val();
        var fatherName = $('#fatherName').val();
        var fatherPhone = $('#fatherPhone').val();
        var motherName = $('#motherName').val();
        var motherPhone = $('#motherPhone').val();
        var guardName = $('#guardName').val();
        var guardPhone = $('#guardPhone').val();
        var phone = /(^[1][3,4,5,7,8][0-9]{9}$)|(^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$)/;//电话或手机号
        if ((fatherName.length !==0 && fatherPhone.length !== 0 ) || (motherName.length !==0 && motherPhone.length !== 0 ) || (guardName.length !==0 && guardPhone.length !== 0 )){
            if (fatherPhone.length !== 0){
                var isPhone1 = phone.test(fatherPhone);
                if (isPhone1 === false){
                    window.top.xhTop.showError('请填入爸爸的正确手机号');
                    xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                    return false;
                }
            }
            if(motherPhone.length !== 0){
                var isPhone2 = phone.test(motherPhone);
                if (isPhone2 === false){
                    window.top.xhTop.showError('请填入妈妈的正确手机号');
                    xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                    return false;
                }
            }
            if(guardPhone.length !== 0){
                var isPhone3 = phone.test(guardPhone);
                if (isPhone3 === false){
                    window.top.xhTop.showError('请填入监护人的正确手机号');
                    xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                    return false;
                }
            }
            xhExtra.ajaxRequest('device/saveRepairData', {userId:userId, logonName:logonName, userName:userName,sImei:sImei,schoolId:schoolId,
                    fatherName:fatherName, motherName:motherName,guardName:guardName,motherPhone:motherPhone,fatherPhone:fatherPhone,guardPhone:guardPhone,
                    sDeviceNumber:$('#sDeviceNumber').val(),sModel:$('#sModel').val(),sBrand:$('#sBrand').val(),remark:$('#remark').val(),
                    iDestination:$('#iDestination').val(),expressCompany:$('#expressCompany').val(),repairDate:$('#repairDate').val(),expressSend:$('#expressSend').val(),
                    purpose:$('#purpose').val()
                },
                {success: true,check:false},function (res) {
                    if(res.code === 200){
                        window.top.swal('成功',res.msg,'success');
                        window.top.xhTop.closeModal();
                    } else {
                        window.top.xhTop.showError(res.error);
                        xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                    }
                });
        }else{
            window.top.xhTop.showError('至少完整填写一个父母或监护人的信息！');
            xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
        }
    });
});

